﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IceDog.DNL.CSharp.Grammar.FormatProvider
{
    /// <summary>
    /// 任意进制转换器
    /// </summary>
    public class AnyRadixConvert
    {
        /// <summary>
        /// 转换为任意的进制
        /// </summary>
        /// <param name="argToConvert">参数</param>
        /// <param name="formatStr">格式化字符串,如Ra2</param>
        public static void ConvertToAnyRadix(object argToConvert,string formatStr)
        {
            AnyRadix provider = new AnyRadix();
            string messageStr =String.Format("{{0:{0}}}", formatStr);

            // Write the first part of the output line.
            Console.Write("{0,18}  {1,-6}", argToConvert, formatStr);

            // Convert the specified argument using the specified format.
            try
            {
                Console.WriteLine(String.Format(provider, messageStr, argToConvert));
            }
            catch (Exception ex)
            {
                // Display the exception without the stack trace.
                int lineEnd = ex.ToString().IndexOf('\n');
                Console.WriteLine("{0}\n",ex.ToString().Substring(0, lineEnd));
            }
        }
    }
}
